import { useIntersectionObserver, useIntervalFn } from '@vueuse/core'
import { ref } from 'vue'
const useLazyData = fn => {
  const target = ref(null)
  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        stop()
        fn()
      }
    },
    { threshold: 0 }
  )
  return target
}
// 倒计时封装
const useCountDown = (num) => {
  const time = ref(0)
  // pause: 停止
  // resume: 开始
  const { pause, resume } = useIntervalFn(() => {
    time.value--
    if (time.value <= 0) {
      pause()
    }
  }, 1000, { immediate: false })
  const start = (num) => {
    time.value = num
    resume()
  }
  const stop = () => {
    pause()
  }
  return { time, start, stop }
}
export { useLazyData, useCountDown }
